package com.qiye.pointsserver.service.impl;


import com.qiye.pointsserver.config.properties.EmailProperties;
import com.qiye.pointsserver.service.EmailService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.Properties;

@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Resource
    private EmailProperties emailProperties;

    @Override
    public void sendSimpleMessage(String email, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置发件人
        message.setFrom("3092934142@qq.com");
        // 设置收件人
        message.setTo(email);
        // 设置邮件主题
        message.setSubject(subject);
        // 设置邮件内容
        message.setText(text);
        // 发送邮件
        mailSender().send(message);
    }

    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(emailProperties.getHost());
        mailSender.setPort(emailProperties.getPort());
        mailSender.setUsername(emailProperties.getUsername());
        mailSender.setPassword(emailProperties.getPassword());
        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "false");  // 关闭TLS
        props.put("mail.smtp.ssl.enable", "true");  // 启用SSL
        props.put("mail.smtp.socketFactory.port", "465");  // 端口配置
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  // SSL工厂
        props.put("mail.smtp.socketFactory.fallback", "false");
        return mailSender;
    }
}
